//
//  UIView+Assist.m
//  Amily
//
//  Created by 张鹏 on 16/1/7.
//  Copyright © 2016年 Amily. All rights reserved.
//

#import "UIView+Assist.h"

@implementation UIView (Assist)

static char  ResponseViewKey;


- (void)configResponseReveal:(ALResponsePageType)page
                     hasData:(BOOL)hasData
                    netError:(id)error
                 reloadBlock:(void (^)(id sender))block
{

    if (hasData) {

        if (self.responseView) {
            self.responseView.hidden = YES;
            [self.responseView removeFromSuperview];
        }
        
    }else{

        if (!self.responseView) {
            self.responseView  = [[ALResponseView alloc] init];
        }
        self.responseView.backgroundColor = [UIColor whiteColor];
        self.responseView.hidden = NO;
        [[self blankPageContainer] addSubview:self.responseView];
        CGFloat topSpace = 0;
        if (page == ResponsePageHome || page == ResponsePageOrder) {
            topSpace = 0;
        }else if(page == ResponsePageAround || page == ResponsePageArticleList)
        {
            topSpace = 0;
        }
        else if (page == ResponsePageShopProducts)
        {
            topSpace = 105;
        } else if (page == ResponsePageNearest){
            topSpace = 60;
        }else{
            topSpace = 64;
        }
        [self.responseView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self).with.insets(UIEdgeInsetsMake(topSpace, 0, 0, 0));
        }];
        
        [self.responseView configResponseView:page hasData:hasData netError:error reloadBlock:block];
    }
}


- (UIView *)blankPageContainer{
    UIView *blankPageContainer = self;
    for (UIView *aView in [self subviews]) {
        if ([aView isKindOfClass:[UITableView class]]) {
            blankPageContainer = aView;
        }
    }
    return blankPageContainer;
}

- (void)setResponseView:(ALResponseView *)responseView{
    [self willChangeValueForKey:@"ALResponseView"];
    objc_setAssociatedObject(self, &ResponseViewKey,
                             responseView,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self didChangeValueForKey:@"ALResponseView"];
}

- (ALResponseView *)responseView{
    return objc_getAssociatedObject(self, &ResponseViewKey);
}


@end
